/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.admin.modeljpa;

import com.admin.modeljpa.exceptions.NonexistentEntityException;
import com.admin.modeljpa.exceptions.PreexistingEntityException;
import com.admin.modeljpa.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.entities.EventLibrary;
import com.entities.Employee;
import com.entities.EventGift;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Tuan Long
 */
public class EventGiftJpaController implements Serializable {

    public EventGiftJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(EventGift eventGift) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            EventLibrary eventId = eventGift.getEventId();
            if (eventId != null) {
                eventId = em.getReference(eventId.getClass(), eventId.getEventId());
                eventGift.setEventId(eventId);
            }
            Employee username = eventGift.getUsername();
            if (username != null) {
                username = em.getReference(username.getClass(), username.getUsername());
                eventGift.setUsername(username);
            }
            em.persist(eventGift);
            if (eventId != null) {
                eventId.getEventGiftCollection().add(eventGift);
                eventId = em.merge(eventId);
            }
            if (username != null) {
                username.getEventGiftCollection().add(eventGift);
                username = em.merge(username);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findEventGift(eventGift.getId()) != null) {
                throw new PreexistingEntityException("EventGift " + eventGift + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(EventGift eventGift) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            EventGift persistentEventGift = em.find(EventGift.class, eventGift.getId());
            EventLibrary eventIdOld = persistentEventGift.getEventId();
            EventLibrary eventIdNew = eventGift.getEventId();
            Employee usernameOld = persistentEventGift.getUsername();
            Employee usernameNew = eventGift.getUsername();
            if (eventIdNew != null) {
                eventIdNew = em.getReference(eventIdNew.getClass(), eventIdNew.getEventId());
                eventGift.setEventId(eventIdNew);
            }
            if (usernameNew != null) {
                usernameNew = em.getReference(usernameNew.getClass(), usernameNew.getUsername());
                eventGift.setUsername(usernameNew);
            }
            eventGift = em.merge(eventGift);
            if (eventIdOld != null && !eventIdOld.equals(eventIdNew)) {
                eventIdOld.getEventGiftCollection().remove(eventGift);
                eventIdOld = em.merge(eventIdOld);
            }
            if (eventIdNew != null && !eventIdNew.equals(eventIdOld)) {
                eventIdNew.getEventGiftCollection().add(eventGift);
                eventIdNew = em.merge(eventIdNew);
            }
            if (usernameOld != null && !usernameOld.equals(usernameNew)) {
                usernameOld.getEventGiftCollection().remove(eventGift);
                usernameOld = em.merge(usernameOld);
            }
            if (usernameNew != null && !usernameNew.equals(usernameOld)) {
                usernameNew.getEventGiftCollection().add(eventGift);
                usernameNew = em.merge(usernameNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = eventGift.getId();
                if (findEventGift(id) == null) {
                    throw new NonexistentEntityException("The eventGift with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            EventGift eventGift;
            try {
                eventGift = em.getReference(EventGift.class, id);
                eventGift.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The eventGift with id " + id + " no longer exists.", enfe);
            }
            EventLibrary eventId = eventGift.getEventId();
            if (eventId != null) {
                eventId.getEventGiftCollection().remove(eventGift);
                eventId = em.merge(eventId);
            }
            Employee username = eventGift.getUsername();
            if (username != null) {
                username.getEventGiftCollection().remove(eventGift);
                username = em.merge(username);
            }
            em.remove(eventGift);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<EventGift> findEventGiftEntities() {
        return findEventGiftEntities(true, -1, -1);
    }

    public List<EventGift> findEventGiftEntities(int maxResults, int firstResult) {
        return findEventGiftEntities(false, maxResults, firstResult);
    }

    private List<EventGift> findEventGiftEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(EventGift.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public EventGift findEventGift(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(EventGift.class, id);
        } finally {
            em.close();
        }
    }

    public int getEventGiftCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<EventGift> rt = cq.from(EventGift.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
